package com.linked.goods.goodsbase.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.linked.goods.goodsbase.bean.dto.GoodsKindDTO;
import com.linked.goods.goodsbase.bean.po.GoodsKindPO;
import com.linked.goods.goodsbase.service.IGoodsKindService;
import com.linked.universal.common.LinkedTree;
import com.linked.universal.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sun.reflect.generics.tree.Tree;

/**
 * @author :dbq
 * @date : 2023/5/17 9:00
 * @description : desc
 */
@RestController
@Slf4j
@RequestMapping("/goodstype")
public class GoodsTypeController {

    private final IGoodsKindService goodsKindService;

    private final ObjectMapper mapper;

    public GoodsTypeController(IGoodsKindService goodsKindService, ObjectMapper mapper) {
        this.goodsKindService = goodsKindService;
        this.mapper = mapper;
    }

    @PostMapping("addGoodsKind")
    Result addGoodsKind(@RequestBody GoodsKindDTO param) throws Exception {
        if (log.isInfoEnabled()) {
            log.info("添加商品种类接口 入参：{}", mapper.writeValueAsString(param));
        }

        String kindId = null;
        try {
            kindId = goodsKindService.addGoodsKind(new GoodsKindPO(param));
        } catch (Exception e) {
            log.error("添加商品种类接口 异常！", e);
            return Result.error("未知异常，请联系管理员！");
        }
        return !StringUtils.isEmpty(kindId) ? Result.success(kindId) : Result.ok(false, "添加失败!");
    }

    @PostMapping("queryKindTree")
    Result queryKindTree(String kindPid) {
        if (StringUtils.isEmpty(kindPid)){
            kindPid = "0";
        }

        try {
            LinkedTree<GoodsKindPO> tree = goodsKindService.queryKindTree(kindPid);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return Result.success();
    }

}
